//@author A0097091Y
//Unit test for storage component

package crossoff.db;

import static org.junit.Assert.*;

import java.io.IOException;

import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.junit.Test;

import crossoff.model.Task;

public class StorageTest {

	@Test
	public void testFloating() throws IOException {
		
		Storage store = Storage.getInstance();
		
		Task testFloating = Storage.getInstance().saveFloating("test floating");
		int testFloatingID = testFloating.getTaskID();
		
		//GET TASK OBJECT
		//Boundary case where specified taskID does not exist
		assertEquals("Get Task, ID does not exist", null, store.getTaskFromID(testFloatingID+1));
		//Boundary case where taskID exists
		assertEquals("Get Task, ID exists", testFloating,store.getTaskFromID(testFloatingID));
		
		
		
		//GET TASK DESCRIPTION
		//Case where specified taskID does not exist
		assertEquals("Get Task description, ID does not exist", null, store.fetchDescription(testFloatingID+1));
		//Case where specified taskID exists
		assertEquals("Get Task description, ID exists", "test floating", store.fetchDescription(testFloatingID));
		
		
		
		//GET EDIT STRING (String that is displayed to user when they want to edit task)
		//Case where specified taskID does not exist
		assertEquals("Get edit string, ID does not exist", null, store.getEditDescription(testFloatingID+1));
		//Case where specified taskID exists
		assertEquals("Get edit string, ID exists", "test floating", store.getEditDescription(testFloatingID));
		
		
		
		//DELETE TASK
		//Case where specified taskID does not exist
		assertEquals("Delete, ID does not exist", false, store.deleteTask(testFloatingID+1));
		//Case where specified taskID exists
		assertEquals("Delete, ID exists", true, store.deleteTask(testFloatingID));
		assertEquals("Get task after deletion", null, store.fetchDescription(testFloatingID));
	}
	
	@Test
	public void testFromSingleDay() throws IOException {
		
		Storage store = Storage.getInstance();
		LocalTime testStart = new LocalTime(0, 0);
		LocalTime testEnd = new LocalTime(1, 0);
		LocalDate testDate = new LocalDate(2014, 1, 1);
		Task testFromSingleDay = store.saveFromTimedSingleDay("test from single day", testStart, testEnd, testDate);
		int testFromSingleDayID = testFromSingleDay.getTaskID();
		
		//GET TASK OBJECT
		//Boundary case where specified taskID does not exist
		assertEquals("Get Task, ID does not exist", null, store.getTaskFromID(testFromSingleDayID+1));
		//Boundary case where taskID exists
		assertEquals("Get Task, ID exists", testFromSingleDay,store.getTaskFromID(testFromSingleDayID));
		
		
		
		//GET TASK DESCRIPTION
		//Case where specified taskID does not exist
		assertEquals("Get Task description, ID does not exist", null, store.fetchDescription(testFromSingleDayID+1));
		//Case where specified taskID exists
		assertEquals("Get Task description, ID exists", "test from single day", store.fetchDescription(testFromSingleDayID));
		
		
		
		//GET EDIT STRING (String that is displayed to user when they want to edit task)
		//Case where specified taskID does not exist
		assertEquals("Get edit string, ID does not exist", null, store.getEditDescription(testFromSingleDayID+1));
		//Case where specified taskID exists
		assertEquals("Get edit string, ID exists", "test from single day from 0:00 to 1:00 on 1-1-2014", store.getEditDescription(testFromSingleDayID));
		
		
		
		//DELETE TASK
		//Case where specified taskID does not exist
		assertEquals("Delete, ID does not exist", false, store.deleteTask(testFromSingleDayID+1));
		//Case where specified taskID exists
		assertEquals("Delete, ID exists", true, store.deleteTask(testFromSingleDayID));
		assertEquals("Get task after deletion", null, store.fetchDescription(testFromSingleDayID));
	}
	
	@Test
	public void testAtTimed() throws IOException {
		
		Storage store = Storage.getInstance();
		LocalTime testStart = new LocalTime(0, 0);
		LocalDate testDate = new LocalDate(2014, 1, 1);
		Task testAtTimed = store.saveAtTimed("test at timed", testStart, testDate);
		int testAtTimedID = testAtTimed.getTaskID();
		
		//GET TASK OBJECT
		//Boundary case where specified taskID does not exist
		assertEquals("Get Task, ID does not exist", null, store.getTaskFromID(testAtTimedID+1));
		//Boundary case where taskID exists
		assertEquals("Get Task, ID exists", testAtTimed,store.getTaskFromID(testAtTimedID));
		
		
		
		//GET TASK DESCRIPTION
		//Case where specified taskID does not exist
		assertEquals("Get Task description, ID does not exist", null, store.fetchDescription(testAtTimedID+1));
		//Case where specified taskID exists
		assertEquals("Get Task description, ID exists", "test at timed", store.fetchDescription(testAtTimedID));
		
		
		
		//GET EDIT STRING (String that is displayed to user when they want to edit task)
		//Case where specified taskID does not exist
		assertEquals("Get edit string, ID does not exist", null, store.getEditDescription(testAtTimedID+1));
		//Case where specified taskID exists
		assertEquals("Get edit string, ID exists", "test at timed at 0:00 on 1-1-2014", store.getEditDescription(testAtTimedID));
		
		
		
		//DELETE TASK
		//Case where specified taskID does not exist
		assertEquals("Delete, ID does not exist", false, store.deleteTask(testAtTimedID+1));
		//Case where specified taskID exists
		assertEquals("Delete, ID exists", true, store.deleteTask(testAtTimedID));
		assertEquals("Get task after deletion", null, store.fetchDescription(testAtTimedID));
	}
}
